//
//  HYXUnitTestNode.m
//  SpeSmartUnitTester_Example
//
//  Created by points on 2020/8/16.
//  Copyright © 2020 hfqf123. All rights reserved.
//

#import "HYXUnitTestNode.h"

@implementation HYXUnitTestNode


/// 构建父节点指针
- (void)rebuildParentNode{
    if(self.LeftChild){
        self.LeftChild.parent = self;
        [self.LeftChild rebuildParentNode];
    }
    
    if(self.RightChild){
        self.RightChild.parent = self;
        [self.RightChild rebuildParentNode];
    }
}

/// 执行当前节点操作，如页面跳转，提交操作
- (void)excuteNode{
    NSLog(@"excuteNode---->%@",[self.Data yy_modelToJSONObject]);
    if(self.ParentId == -1){
        UINavigationController *navi = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        self.navi = navi;
        self.root = self;
    }
    self.excuted = YES;
    
    if(self.Data.NodeType == 1){
           SEL sel = NSSelectorFromString(self.Data.ActionName);
           self.controller.unitTestNode = self;
           [self.controller.vm performSelector:sel withObject:nil afterDelay:self.Data.Delay];
        
           //任何页面跳转后需要执行mock data接口
           SEL mockSel = NSSelectorFromString(@"onGetMockData");
           [self.controller performSelector:mockSel withObject:nil afterDelay:self.Data.Delay+0.5];
    }else{
       HYXRouterControllerModel *target = [HYXRouterControllerModel from:self.Data.PageName
                                                                     navi:self.navi
                                                          objectPropertys:@{
                                                              @"mockData":self.Data.MockData?self.Data.MockData:@{},
                                                              @"unitTestNode":self,
                                                              @"onUnitTestCommitedBlock":^(HYXUnitTestNode *unitTestNode,HYXUnitTestBaseViewController *vc){
           HYXUnitTestNode *next = [HYXUnitTestNode getNextNode:unitTestNode];
           next.controller = vc;
           next.navi = vc.navigationController;
           [next excuteNode];
           if(self.root.nextNodeBlock){
               self.root.nextNodeBlock(next);
           }
        }
                                                          }
                                                             intPropertys:nil
                                                            boolPropertys:@{
                                                                Router_HidesBottomBar:@(YES)
                                                            }];
        HYXROUTER.open(target).then(^(HYXRouterSucceed * resp) {
            HYXUnitTestNode *next = [HYXUnitTestNode getNextNode:self];
            next.controller = resp.vc;
            if(self.root.nextNodeBlock){
                self.root.nextNodeBlock(next);
            }
            [next performSelector:@selector(excuteNode) withObject:nil afterDelay:next.Data.Delay];
        });
    }
  
}


- (void)autoExcute{
    [self excuteNode];
    
    if(self.LeftChild){
        [self.LeftChild autoExcute];
    }
    
    if(self.RightChild){
        [self.RightChild autoExcute];
    }
}


//- (HYXUnitTestNode *)getNextNode{
//    if(!self.excuted){
//        return self;
//    }
//
//    if(self.LeftChild){
//        self.LeftChild.navi = self.navi;
//        self.LeftChild.root = self.root;
//       return [self.LeftChild getNextNode];
//    }
//
//    if(self.RightChild){
//        self.RightChild.navi = self.navi;
//        self.RightChild.root = self.root;
//       return [self.RightChild getNextNode];
//    }
//    return nil;
//}

/// 先序排序下获取某个节点后的下个执行节点
/// @param root root
+ (HYXUnitTestNode *)getNextNode:(HYXUnitTestNode*)node{
    if(!node){
        return node;
    }
    
    if(node.ParentId==-1&&!node.excuted){
        return node;
    }
    
    if(node.LeftChild){
        node.LeftChild.navi = node.navi;
        node.LeftChild.root = node.root;
        return node.LeftChild;
    }else if (node.RightChild){
        node.RightChild.navi = node.navi;
        node.RightChild.root = node.root;
        return node.RightChild;
    }else if (node.parent&&node==node.parent.LeftChild){
        HYXUnitTestNode *parent = node.parent;
        if(!parent.RightChild){
            return nil;
        }
        node.RightChild.navi = node.navi;
        node.RightChild.root = node.root;
        return parent.RightChild;
    }else if (node.parent&&node==node.parent.RightChild){
        HYXUnitTestNode *parent = node.parent;
        HYXUnitTestNode *current = node;
        while (parent&&current==parent.RightChild) {
            current = parent;
            parent = parent.parent;
        }
        if(!parent.RightChild){
            return nil;
        }
        node.RightChild.navi = node.navi;
        node.RightChild.root = node.root;
        return parent.RightChild;
    }
    return nil;
}

@end
